<cfcomponent displayname="Termination" output="true">
    
    <cfproperty name="DOMAINCONTROLLER" default="10.100.1.101" />
    <cfproperty name="ORG_SCF" default="117" />
    <cfproperty name="DOLLARS" default="2" />
    <cfproperty name="SERVICE_ACCOUNT" default="SCFAdmin" />
    <cfproperty name="Error" default="False" />
    <cfproperty name="EmpStatusReason" default="" />
    <cfproperty name="TermReqID" default="-1" />
    <cfproperty name="Src" defualt="" />
    <cfproperty name="Dest" defualt="" />
    <cfproperty name="Home" defualt="" />
    <cfproperty name="ArchiveDir" defualt="" />
    <cfproperty name="ArchiveEmailDir" defualt="" />
    <cfproperty name="ErrorMessage" defualt="" />
    <cfproperty name="RootArchiveDir" defualt="\\scfappshare\ITS_Data\Archive\" />
    <cfproperty name="Manual" defualt="0" />
    
    
	<cffunction name="init" access="public" output="false" returntype="Termination">
        <cfargument name="TermReqID" type="string" required="no" default="-1">
        <cfargument name="Manual" type="numeric" required="no" default="0">
        
        <cfobject type="dotnet" name="objQuery" class="ADTools.Query" assembly ="C:\Program Files\Southcentral Foundation\ADTools\ADTools.dll">  
        <cfobject type="dotnet" name="objMod" class="ADTools.Modify" assembly ="C:\Program Files\Southcentral Foundation\ADTools\ADTools.dll"> 
        <cfobject type="dotnet" name="objTerm" class="ADTools.Termination" assembly ="C:\Program Files\Southcentral Foundation\ADTools\ADTools.dll">
        
        <cfset this.Manual = False />
        <cfif ARGUMENTS.Manual eq 1>
            <cfset this.Manual = True />
        <cfelse>
            <cfset this.Manual = False />
        </cfif>
        <cfif ARGUMENTS.TermReqID NEQ -1>
            <cfset setTermReqID(ARGUMENTS.TermReqID) />
        </cfif>
		<cfreturn this />
	</cffunction>    
      
        
    <cffunction name="getTermReqID" access="public" output="false" returntype="string">
        <cfreturn this.TermReqID />
    </cffunction>
    
    
    <cffunction name="setTermReqID" access="public" output="false" returntype="void">
        <cfargument name="TermReqID" type="string" required="true" default="-1" />
        <cftry>
            <cfinclude template="/code_lib/SCF_Employee.cfm">
            <cfinclude template="/code_lib/SCF_HR.cfm">
            <cfinclude template="/code_lib/SCF_REGEX.cfm">
	        <cfset BASE = CreateObject("component", "base") />
	        <cfset mainQueries = CreateObject("component", "queries") /> 
	        <cfset termQueries = CreateObject("component", "terminationQueries") />
	        <cfset this.TermReqID = ARGUMENTS.TermReqID />        
	        <cfset this.GetTermDetails = mainQueries.funcGetTermDetails( this.TermReqID, 22 ) />        
	        <cfset this.EmployeeObj = SCF_Employee_GetEmployee(this.GetTermDetails.RosettaID) />
	        <cfset this.RequestorObj = SCF_Employee_GetEmployee(this.GetTermDetails.InitialTermReqByID) />
	        <cfset this.SupervisorID = SCF_HR_WhosTheBoss(this.GetTermDetails.RosettaID) />
	        <cfset this.SupervisorObj = SCF_Employee_GetEmployee(this.SupervisorID) /> 
	        <cfset this.RehireEligible = this.GetTermDetails.RehireElig />        
	        <cfset this.ScheduledTerm = "" />
	        <cfset this.SysScheduledTerm = "" />
            <cfset this.RootArchiveDir = "\\scfappshare\ITS_Data\Archive\">
	        <cfset this.Home = "\\scfusershare\home\#this.EmployeeObj.Login#\">
	        <cfset this.ArchiveDir = "\\scfappshare\ITS_Data\Archive\#this.EmployeeObj.Login##DateFormat(now(), 'YYYYMMDD')#" />
	        <cfset this.ArchivedUsersContainer = GetUserDNArchivedContainer( this.EmployeeObj.login ).ArchivedUsersContainer />
	        <cfset this.UserDN = GetUserDNArchivedContainer( this.EmployeeObj.login ).UserDN />
	        <cfset this.ArchiveEmailDir = this.ArchiveDir & "\Email" />
	        <cfset this.EmpStatusReason = "" />
	        <cfset this.SERVICE_ACCOUNT = "SCFAdmin" />
	        <cfset this.DOMAINCONTROLLER = "10.100.1.101"/>
	        <cfset this.ORG_SCF = "117" />
	        <cfset this.DOLLARS = "2" />        
	        <cfif ListGetAt(this.EmployeeObj.ExtendedLogin, 1, "\") EQ "SCF">
	            <cfset this.SCFAccount = True />
	        <cfelse>        
	            <cfset this.SCFAccount = False />       
	        </cfif>
                            
	        <cfif dateFormat( DateAdd('d', 1, this.GetTermDetails.TermDate) , "m/d/yyyy") GT dateFormat(now(), "m/d/yyyy") >
	            <cfset this.Scheduled = True />
                <cfset this.TermVerbage = "The Termination has been scheduled for " & dateFormat(this.GetTermDetails.TermDate, "m/d/yyyy") & " and no further action is required on your part." />
                <cfset this.TermBody = "Scheduled" />
	            <cfif termQueries.funcGetScheduledTerms(this.TermReqID, DateAdd('d', 1, this.GetTermDetails.TermDate)).RecordCount eq 0>
	                <cfset this.ScheduledTerm = termQueries.funcInsertTermSchedule(this.TermReqID, dateFormat( DateAdd('d', 1, this.GetTermDetails.TermDate) , "m/d/yyyy")) />	                
	            </cfif>
                <cfset this.ScheduledTerm = termQueries.funcGetScheduledTerms( this.TermReqID, DateAdd('d', 1, this.GetTermDetails.TermDate) ) />
	        <cfelse>        
	            <cfset this.Scheduled = False />
                <cfset this.TermVerbage = "" />
                <cfset this.TermBody = "Completed" />
	            <cfset this.ScheduledTerm = termQueries.funcGetScheduledTerms( this.TermReqID, DateAdd('d', 1, this.GetTermDetails.TermDate) ) />    
	        </cfif>
	        
	        <cfif dateFormat(this.GetTermDetails.SysTermDate, "m/d/yyyy") GT dateFormat(now(), "m/d/yyyy")>
	            <cfset this.SysScheduled = True />
                <cfset this.SysTermVerbage = "The Termination has been scheduled for " & dateFormat(this.GetTermDetails.SysTermDate, "m/d/yyyy") & " and no further action is required on your part." />
                <cfset this.TermBody = "Scheduled" />
	            <cfif termQueries.funcGetSysScheduledTerms(this.TermReqID, DateAdd('d', 1, this.GetTermDetails.SysTermDate)).RecordCount eq 0>
	                <cfset this.SysScheduledTerm = termQueries.funcInsertSysTermSchedule(this.TermReqID, dateFormat( DateAdd('d', 1, this.GetTermDetails.SysTermDate) , "m/d/yyyy")) />	                
	            </cfif>
                <cfset this.SysScheduledTerm = termQueries.funcGetSysScheduledTerms( this.TermReqID, DateAdd('d', 1, this.GetTermDetails.SysTermDate) ) />
	        <cfelse>        
	            <cfset this.SysScheduled = False />
                <cfset this.SysTermVerbage = "" />
                <cfset this.TermBody = "Completed" />
	            <cfset this.SysScheduledTerm = termQueries.funcGetSysScheduledTerms( this.TermReqID, DateAdd('d', 1, this.GetTermDetails.SysTermDate) ) />
	        </cfif>
                                    
            <cfif this.Manual>
                <cfset this.Scheduled = False />
                <cfset this.SysScheduled = False />
            </cfif>
            
        <cfcatch type="any">
            <cfset this.ErrorMessage = CFCATCH />
        </cfcatch>
        </cftry>       
        <cfreturn />
    </cffunction>
    
  
    <cffunction name="ADDisable" access="public" output="true" returntype="any">
        <cfset var BASE = CreateObject("component", "base") />
        <cfset var mainQueries = CreateObject("component", "queries") /> 
        <cfset var termQueries = CreateObject("component", "terminationQueries") /> 
        <cfset Var ADResult = '' />
        <cfset var retObj = { Status = 'None', Message = '', Result = 0 } />
            
        <cftry>
        
            <cfif Not this.Scheduled>
                <cfset retObj.Result = funcADDisable(this.EmployeeObj.Login)/>
                <cfset retObj.Status = "Completed" />                
                <cfset retObj.Message = "<strong>Success</strong>" />
            <cfelse>                
                <cfset retObj.Message = "<strong>Scheduled for #dateFormat(this.GetTermDetails.TermDate, 'm/d/yyyy')#</strong>" />
                <cfset retObj.Status = 'Completed' />
            </cfif>

            <cfif retObj.Result NEQ 0>
                <cfset retObj.Message = "" />
                <cfset retObj.Status = 'Fail' />
                <cfset retObj.Message &= '<strong>Error Disabling the AD account: #this.EmployeeObj.Login#<BR /> 
                    Result:  #replace(retObj.Message,"\'","'","all")#<BR />
                    Step ID: #this.GetTermDetails.ActionStepID#<BR />
                    Action by User (Rosetta ID): #Session.intUserRosettaID#</strong>' />
                <cfset ErrorEmail("EPAR Error:  Processing AD account for Termination/Layoff", retObj.Message) />                
            <cfelseif retObj.Message EQ "">
                <cfset retObj.Status = 'Completed' />
                <cfset retObj.Message = "<strong>Success</strong>" />
            </cfif>        

	        <cfcatch type="any">
                <cfset retObj.Status = 'Fail' />
                <cfset retObj.Result &= ' Message: #replace(CFCATCH.message,"'","\'","all")#<BR/>Detail: #replace(CFCATCH.Detail,"'","\'","all")#'  />
                <cfset retObj.Message = '<strong>Error Disabling the AD account: #this.EmployeeObj.Login#<BR /> 
                    Result:  #replace(retObj.Result,"\'","'","all")#<BR />
                    Step ID: #this.GetTermDetails.ActionStepID#<BR />
                    Action by User (Rosetta ID): #Session.intUserRosettaID#</strong><br /><br />
                    Please take the following steps:<BR />
                    1) Disable the Account<Br />
                    2) Scramble the PW<Br />
                    3) Hide from Address List (under "Exchange Advanced")' />
               <cfset ServiceDeskErrorEmail("EPAR Error:  Processing AD account for Termination/Layoff", retObj.Message) />                
                <Cfreturn  retObj />    
	        </cfcatch>                    
        </cftry>
        
        <Cfreturn  retObj />
    </cffunction> 
    
    
    <cffunction name="HomeFolderArchive" access="public" output="true" returntype="any">
        <cfset var BASE = CreateObject("component", "base") />
        <cfset var mainQueries = CreateObject("component", "queries") /> 
        <cfset Var HomeFolderResult = '' />
        <cfset var retObj = { Status = 'None', Message = '', Result = 0 } />
        <cfset var index = 0>
        <cfset Var objResult = { Result = 0, Error = ""} />
            
        <cftry>
            
            <cfif Not this.Scheduled>
                <cfif this.EmpStatusReason NEQ 57><!--- summer layoff's don't need home folder archived --->
                    <cfif DirectoryExists(this.ArchiveDir)>
						<cfset index = 0>
						<cfset this.ArchiveDirTEMP = this.ArchiveDir>						
						<!--- If TRUE then rename the new directory before we attempt to create it. ---> 
						<cfloop condition="DirectoryExists(this.ArchiveDirTEMP) EQ True">						
						  <cfset index += 1>
						  <cfset this.ArchiveDirTEMP = this.ArchiveDir & "(#index#)" >						
						</cfloop>						
						<cfset this.ArchiveDir = this.ArchiveDirTEMP>
						<cfset this.ArchiveEmailDir = this.ArchiveDir & "\Email" >
                    </cfif>
                    
                    <cfif Not this.Scheduled>                            
                       <cfdirectory action="create" directory="#this.ArchiveDir#">
                       <cfdirectory action="create" directory="#this.ArchiveEmailDir#">                   
                   </cfif>
                   
					<cfset objResult = funcArchiveHomeFolder(this.Home, this.ArchiveDir) />
					<cfset HomeFolderResult = objResult.Result />
                    <cfset retObj.Result = objResult.Result />                    
                    <cfset retObj.Message = '<strong>Success</strong>'/>                    
                    <cfset retObj.Status = 'Completed'/>                                     
                </cfif>
                
            <cfelse>
                <cfif this.EmpStatusReason NEQ 57>
                    <cfset retObj.Message = "<strong>Scheduled for #dateFormat(this.GetTermDetails.TermDate, 'm/d/yyyy')#</strong>"/>
                    <cfset retObj.Result = HomeFolderResult />
                    <cfset retObj.Status = 'Completed'/>
                <cfelse>
                    <cfset retObj.Message = "<strong>Summer Layoff.  Home Folder Archive N/A.</strong>"/>
                    <cfset retObj.Result = HomeFolderResult />
                    <cfset retObj.Status = 'Completed'/>
                </cfif>
            </cfif>
             
            <cfcatch type="any">
                <cfset retObj.Status = 'Fail' />
                <cfset retObj.Result = ' Message: #replace(CFCATCH.message,"'","\'","all")#<BR/>Detail: #replace(CFCATCH.Detail,"'","\'","all")#' />
                <cfset retObj.Message = 'Error Archiving Home Directory: <BR />#replace(retObj.Result,"\'","'","all")#<BR /><BR />
                        Step ID: #this.GetTermDetails.ActionStepID#<BR />
                        Action by User (Rosetta ID): #Session.intUserRosettaID#'  />                
                <cfset mainQueries.funcInsertErrorLog(#this.GetTermDetails.ActionStepID#, '#Session.intUserRosettaID#', 
                        'Error Archiving Home Directory: #replace(retObj.Result,"\'","'","all")#')/>
                <cfset ServiceDeskErrorEmail("EPAR Error:  Archiving Home Directory", 
                        'Error Archiving Home Directory: <BR />#replace(retObj.Result,"\'","'","all")#<BR /><BR />
                        Step ID: #this.GetTermDetails.ActionStepID#<BR />
                        Action by User (Rosetta ID): #Session.intUserRosettaID#<BR /><BR />
                        Please Archive the Home Folder manually') />                
                <Cfreturn  retObj />    
            </cfcatch>                    
        </cftry>
        
        <Cfreturn  retObj />
    </cffunction>
    
    
    <cffunction name="HRDisable" access="public" output="true" returntype="any">
        <cfset var BASE = CreateObject("component", "base") />
        <cfset var mainQueries = CreateObject("component", "queries") /> 
        <cfset Var HRResult = '' />
        <cfset var ActionID = 0>
        <cfset var SQL = "">
        <cfset var retObj = { Status = 'None', Message = '', Result = 0 } />
        <cfset Var objResult = { Result = 0, Error = ""} />        
            
        <cftry>
    
            <cfif this.GetTermDetails.EmpStatusIDNo NEQ -10132>            
                <cfset ActionID = 5><!--- Terminate (see tActions)--->                                                    
            <cfelse>                                    
                <cfset ActionID = 2><!--- Place on Leave (see tActions)--->                                                    
            </cfif>           
            
            <cfif Not this.SysScheduled>
                <cfset objResult = funcHRTerm(
                    this.EmployeeObj.HR_PersonIDNo, 
                    this.GetTermDetails.EmpStatusIDNo, 
                    this.ORG_SCF,
                    this.RehireEligible,
                    this.GetTermDetails.TermTypeID,
                    this.GetTermDetails.EmpStatusReasonCode,
                    ActionID,
                    DateFormat(this.GetTermDetails.TermDate,'m/d/yyyy'),
                    DateFormat(this.GetTermDetails.COBRAEndOfMonth,'m/d/yyyy')) />                                             
                <cfset retObj.Result = objResult.Result />                 
                <cfset retObj.SQL = objResult.SQL />
                <cfset retObj.Status = 'Completed' />
                <cfset retObj.Message = "<strong>Success</strong>" />             
            <cfelse>
                <cfset retObj.Status = 'Completed' />
                <cfset retObj.Message = "<strong>Scheduled for #DateFormat(this.GetTermDetails.SysTermDate,'m/d/yyyy')#</strong>" />                 
            </cfif>
            
            <cfif retObj.Result EQ 0>
                <!--- <cfset retObj.Status = 'Completed' />
                <cfset retObj.Message = "<strong>Success</strong>" /> --->
            <cfelse>
                <cfset HRResult = "The termination action on HR record was not successful.  Please check the record in the Kronos HR tools and" & " fix manually if necessary.<BR/>" />
                <cfset retObj.Result = HRResult />
            </cfif>
             
            <cfcatch type="any">
                <cfset retObj.Status = 'Fail' />
                <cfset retObj.Result &= ' Message: #replace(CFCATCH.message,"'","\'","all")#<BR/>Detail: #replace(CFCATCH.Detail,"'","\'","all")#' />
                <cfset retObj.Message = 'Error Terminating the Kronos HR Prod record: #replace(retObj.Result,"\'","'","all")#<BR /><BR />
                        Step ID: #this.GetTermDetails.ActionStepID#<BR />
                        Action by User (Rosetta ID): #Session.intUserRosettaID#'  />                
                <cfset mainQueries.funcInsertErrorLog(#this.GetTermDetails.ActionStepID#, '#Session.intUserRosettaID#', 
                        'Error Terminating the Kronos HR Prod record: #replace(retObj.Result,"\'","'","all")#')/>
                <cfset ErrorEmail("EPAR Error:  Terminating Kronos HR Prod Record", 
                        'Error Terminating the Kronos HR Prod record: #replace(retObj.Result,"\'","'","all")#<BR /><BR />
                        Step ID: #this.GetTermDetails.ActionStepID#<BR />
                        Action by User (Rosetta ID): #Session.intUserRosettaID#<BR /><BR />') />                
                <Cfreturn  retObj />    
            </cfcatch>                    
        </cftry>
        
        <Cfreturn  retObj />
    </cffunction>
    
    
    <cffunction name="TimeKeeperDisable" access="public" output="true" returntype="any">
        <cfset var BASE = CreateObject("component", "base") />
        <cfset var mainQueries = CreateObject("component", "queries") /> 
        <cfset Var TimeKeeperResult = '' />
        <cfset var retObj = { Status = 'None', Message = '', Result = 0 } />

        <cftry>
    
            <cfif Not this.SysScheduled>
                <cfif this.EmpStatusReason NEQ 57><!--- summer layoff check--->
                    <cfset funcDisableTimeKeeper(this.EmployeeObj.TimeKeeper_IDNo)>
                    <cfset retObj.Message = '<strong>Success</strong>' />
                    <cfset retObj.Status = 'Completed' />
                </cfif>
            <cfelse>
                <cfif this.EmpStatusReason NEQ 57> <!--- summer layoff check --->
                    <cfset retObj.Message = "<strong>Scheduled for #dateFormat(this.GetTermDetails.SysTermDate, 'm/d/yyyy')#</strong>" />
                    <cfset retObj.Status = 'Completed' />
                <cfelse>
                    <cfset retObj.Message = "<strong>Summer Layoff.  Disabling TimeKeeper Record N/A.</strong>" />
                    <cfset retObj.Status = 'Completed' />
                </cfif>
            </cfif>
             
            <cfcatch type="any">
                <cfset retObj.Status = 'Fail' />
                <cfset retObj.Result &= ' Message: #replace(CFCATCH.message,"'","\'","all")#<BR/>Detail: #replace(CFCATCH.Detail,"'","\'","all")#' />
                <cfset retObj.Message = 'Error Disabling TimeKeeper Record: <BR />#replace(retObj.Result,"\'","'","all")#<BR /><BR />
                        Step ID: #this.GetTermDetails.ActionStepID#<BR />
                        Action by User (Rosetta ID): #Session.intUserRosettaID#'  />                
                <cfset mainQueries.funcInsertErrorLog(#this.GetTermDetails.ActionStepID#, '#Session.intUserRosettaID#', 
                        'Error Disabling TimeKeeper Record: #replace(retObj.Result,"\'","'","all")#')/>
                <cfset ErrorEmail("EPAR Error:  Disabling TimeKeeper Record", 
                        retObj.Message) />                
                <Cfreturn  retObj />    
            </cfcatch>                    
        </cftry>
        
        <Cfreturn  retObj />
    </cffunction>
    
    
    <cffunction name="EmailExport" access="public" output="true" returntype="any">
        <cfset var BASE = CreateObject("component", "base") />
        <cfset var mainQueries = CreateObject("component", "queries") /> 
        <cfset Var EmailExportResult = '' />
        <cfset Var objResult = { Result = 0, Error = ""} />
        <cfset var retObj = { Status = 'None', Message = '', Result = 0 } />

        <cftry>
    
            <cfif Not this.Scheduled>
                <cfif this.EmpStatusReason NEQ 57><!--- summer layoff check--->
                    <cfif Not DirectoryExists(this.ArchiveEmailDir) >
                        <cfdirectory action="create" directory="#this.ArchiveEmailDir#" />
                    </cfif>                    
                    <cfthread name="MailboxExport_#RandRange(1,10)#" action="run" >
                    <cftry>
                        <cfset funcExportMailbox(
                           this.EmployeeObj.login, 
                           this.ArchiveEmailDir,
                           this.SERVICE_ACCOUNT) />
                    <cfcatch type="any" >
                        <cfset ErrorEmail("Error exporting email for #this.EmployeeObj.login#", "#CFCATCH.Detail#") />
                    </cfcatch>
                    </cftry>
                    </cfthread>
                    <cfset retObj.Result = 0>
                    <cfset retObj.Message = "<strong>Success</strong>" />
                    <cfset retObj.Status = 'Completed' />                    
                </cfif>
            <cfelse>
                <cfif this.EmpStatusReason NEQ 57> <!--- summer layoff check --->
                    <cfset retObj.Message = "<strong>Scheduled for #dateFormat(this.GetTermDetails.TermDate, 'm/d/yyyy')#</strong>" />
                    <cfset retObj.Status = 'Completed' />                            
                <cfelse>
                    <cfset retObj.Message = "<strong>Summer Layoff.  Mailbox Export N/A.</strong>" />
                    <cfset retObj.Status = 'Completed' />                         
                </cfif>
            </cfif>
            
            <cfif objResult.Result NEQ 0>
                <cfset EmailExportResult = replace(replace(replace(replace(objResult.Error, chr(13),'<BR/>','all'), chr(10), '','all'),'<BR/>',''),'<BR/><BR/>','')  />                
                <cfif this.EmpStatusReason NEQ 57><!--- Notify service desk for action to be performed manually--->
                    <cfset AlertEmail("EPAR Termination:  #this.EmployeeObj.FullName# (#this.EmployeeObj.ExtendedLogin#)", 
                        'Please export the mailbox for the following employee:  
                        #this.EmployeeObj.FullName# (Account: #this.EmployeeObj.ExtendedLogin#)
                        <BR /><BR />
                        Error Text: #EmailExportResult#') />
                    <cfset retObj.Message = "<strong>#EmailExportResult#</strong>" />
                    <cfset retObj.Status = 'Fail' />                  
                </cfif>
            <cfelseif objResult.Result EQ "">
                <cfset retObj.Message = "<strong>Success</strong>" />
                <cfset retObj.Status = 'Completed' />     
            </cfif>
             
            <cfcatch type="any">
                <cfset retObj.Status = 'Fail' />
                <cfset retObj.Result = CFCATCH />
                <cfset retObj.Message = "Error" />
                <cfset retObj.Result &= ' Message: #replace(CFCATCH.message,"'","\'","all")#<BR/>Detail: #replace(CFCATCH.Detail,"'","\'","all")#' />
                <cfset retObj.Message = 'Error Exporting mailbox for Termination/Layoff: <BR />#replace(retObj.Result,"\'","'","all")#<BR /><BR />
                        Step ID: #this.GetTermDetails.ActionStepID#<BR />
                        Action by User (Rosetta ID): #Session.intUserRosettaID#'  />                
                <cfset mainQueries.funcInsertErrorLog(#this.GetTermDetails.ActionStepID#, '#Session.intUserRosettaID#', 
                        'Error Exporting mailbox for Termination/Layoff:<BR />#replace(retObj.Result,"\'","'","all")#')/>
                <cfset ServiceDeskErrorEmail("EPAR Error:  Exporting mailbox for Termination/Layoff", 
                        retObj.Message) />                
                <Cfreturn  retObj />    
            </cfcatch>                    
        </cftry>
        
        <Cfreturn  retObj />
    </cffunction>
    
    
    <cffunction name="UserAccountDisable" access="public" output="true" returntype="any">
        <cfset var BASE = CreateObject("component", "base") />
        <cfset var mainQueries = CreateObject("component", "queries") /> 
        <cfset Var ArchAcctResult = '' />
        <cfset Var objResult = { Result = 0, Error = ""} />
        <cfset var retObj = { Status = 'None', Message = '', Result = 0 } />

        <cftry>
    
            <cfif Not this.Scheduled>
                <cfif this.EmpStatusReason NEQ 57><!--- summer layoff check--->
                    <cfset objResults.Result = funcArchiveAndDisable(this.EmployeeObj.login) /> 
                    <cfset retObj.Result = objResults.Result>
                    <cfset retObj.Status = 'Completed' />
                    <cfset retObj.Message = "<strong>Success</strong>" />                          
                </cfif>
            <cfelse>
                <cfif this.EmpStatusReason NEQ 57> <!--- summer layoff check --->
                    <cfset retObj.Message = "<strong>Scheduled for #dateFormat(this.GetTermDetails.TermDate, 'm/d/yyyy')#</strong>" />
                    <cfset retObj.Status = 'Completed' />                            
                <cfelse>
                    <cfset retObj.Message = "<strong>Summer Layoff.  Account Archive N/A.</strong>" />
                    <cfset retObj.Status = 'Completed' />
                </cfif>
            </cfif>
            
            <cfif objResult.Result NEQ 0>
                <cfset ArchAcctResult = replace(replace(replace(replace(objResult.Error, chr(13),'<BR/>','all'), chr(10), '','all'),'<BR/>',''),'<BR/><BR/>','')  />                
                <cfif this.EmpStatusReason NEQ 57><!--- Notify service desk for action to be performed manually--->
                    <cfset AlertEmail("EPAR Termination:  #this.EmployeeObj.FullName# (#this.EmployeeObj.ExtendedLogin#)", 
                        'Please following employee object to the Archived_Users container:  #this.EmployeeObj.FullName# (Account: #this.EmployeeObj.ExtendedLogin#)<BR /><BR />
                        Error Text: #ArchAcctResult#') />
                    <cfset retObj.Message = ArchAcctResult />
                    <cfset retObj.Status = 'Fail' />                  
                </cfif>
            <cfelseif objResult.Result EQ "">
                <cfset retObj.Message = "<strong>Success</strong>" />
                <cfset retObj.Status = 'Completed' />     
            </cfif>
             
            <cfcatch type="any">
                <cfset retObj.Status = 'Fail' />
                <cfset retObj.Result &= ' Message: #replace(CFCATCH.message,"'","\'","all")#<BR/>Detail: #replace(CFCATCH.Detail,"'","\'","all")#' />
                <cfset retObj.Message = 'Error Archiving the Account for Termination/Layoff: #replace(retObj.Result,"\'","'","all")#<BR /><BR />
                         Step ID: #this.GetTermDetails.ActionStepID#<BR />
                         Action by User (Rosetta ID): #Session.intUserRosettaID#'  />                
                <cfset mainQueries.funcInsertErrorLog(#this.GetTermDetails.ActionStepID#, '#Session.intUserRosettaID#', 
                        'Error Archiving the Account for Termination/Layoff: #replace(retObj.Result,"\'","'","all")#')/>
                <cfset ErrorEmail("EPAR Error:  Archiving the Account for Termination/Layoff", 
                        retObj.Message) />                
                <Cfreturn  retObj />    
            </cfcatch>                    
        </cftry>
        
        <Cfreturn  retObj />
    </cffunction>
    
    
    <cffunction name="RosettaDisable" access="public" output="true" returntype="any">
        <cfset var BASE = CreateObject("component", "base") />
        <cfset var mainQueries = CreateObject("component", "queries") /> 
        <cfset Var RosettaResult = '' />
        <cfset Var objResult = { Result = 0, Error = ""} />
        <cfset var retObj = { Status = 'None', Message = '', Result = 0 } />

        <cftry>
    
            <cfif Not this.Scheduled>
                <cfif this.EmpStatusReason NEQ 57><!--- summer layoff check--->
                    <cfset funcDisableRosetta(this.GetTermDetails.RosettaID) />
                    <cfset retObj.Status = 'Completed' />
                    <cfset retObj.Message = "<strong>Success</strong>" />
                </cfif>
            <cfelse>
                <cfif this.EmpStatusReason NEQ 57> <!--- summer layoff check --->
                    <cfset retObj.Message = "<strong>Scheduled for #dateFormat(this.GetTermDetails.TermDate, 'm/d/yyyy')#</strong>" />
                    <cfset retObj.Status = 'Completed' />                            
                <cfelse>
                    <cfset retObj.Message = "<strong>Summer Layoff only - No Rosetta action required.</strong>" />
                    <cfset retObj.Status = 'Completed' />                         
                </cfif>
            </cfif>
            
            <cfmail to="#this.RequestorObj.Email#" bcc="bfrick@scf.cc" from="""EPAR System - Termination"""  type="html" subject="Employee Termination Update">
                <p>The request to <cfif this.EmpStatusReason NEQ 57>terminate the employee<cfelse>place the employee in "Summer Layoff" status</cfif>, 
                    <strong>#this.EmployeeObj.FullName#</strong>, is complete. #this.TermVerbage# Here is some information regarding their action. <BR />
               </p>
                    <table>
                        <tr>
                            <td width=5>&nbsp;</td>
                            <td>Network User Account:</td>
                            <td width=25>&nbsp;</td>
                            <td>#this.EmployeeObj.ExtendedLogin#</td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td> 
                                <cfif this.EmpStatusReason EQ 57>  
                                    Summer Layoff Date: 
                                <cfelse>
                                    Termination Date:
                                </cfif>                         
                            </td>
                            <td>&nbsp;</td>
                            <td>#dateFormat(this.GetTermDetails.TermDate, 'm/d/yyyy')# <strong>(#this.TermBody#)</strong></td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>COBRA Exp Date:</td>
                            <td>&nbsp;</td>
                            <td>#DateFormat(this.GetTermDetails.COBRAEndOfMonth,'m/d/yyyy')#</td>
                        </tr>
                        <tr>
                            <td>&nbsp;</td>
                            <td>Rehire Elig:</td>
                            <td>&nbsp;</td>
                            <td><cfif this.GetTermDetails.RehireElig eq 1>Yes<cfelse>No</cfif></td>
                        </tr>
                    </table>
                    
                </cfmail>
             
            <cfcatch type="any">
                <cfset retObj.Status = 'Fail' />
                <cfset retObj.Result &= ' Message: #replace(CFCATCH.message,"'","\'","all")#<BR/>Detail: #replace(CFCATCH.Detail,"'","\'","all")#' />
                <cfset retObj.Message = 'Error updating the Rosetta Stone record:  #replace(retObj.Result,"\'","'","all")#<BR /><BR />
                         Step ID: #this.GetTermDetails.ActionStepID#<BR />
                         Action by User (Rosetta ID): #Session.intUserRosettaID#'  />                
                <cfset mainQueries.funcInsertErrorLog(#this.GetTermDetails.ActionStepID#, '#Session.intUserRosettaID#', 
                        'Error updating the Rosetta Stone record: #replace(retObj.Result,"\'","'","all")#')/>
                <cfset ErrorEmail("EPAR Error:  Updating Rosetta Stone Record", 
                        retObj.Message) />                
                <Cfreturn  retObj />    
            </cfcatch>                    
        </cftry>
        
        <Cfreturn  retObj />
    </cffunction>
    
       
    <cffunction name="TerminationCompletion" access="public" output="true" returntype="any">
        <cfset var BASE = CreateObject("component", "base") />
        <cfset var mainQueries = CreateObject("component", "queries") />
        <cfset var termQueries = CreateObject("component", "terminationQueries") /> 
        <cfset var retObj = { Status = 'None', Message = '', Result = 0 } />

        <cftry>            
            
            <cfset mainQueries.funcUpdateAction( this.GetTermDetails.ActionStepID, Session.intUserRosettaID, 'Auto Generated' ) />
    
            <cfif Not this.Scheduled AND Not this.SysScheduled >                
                <cfset termQueries.funcInsertOUTermSchedule( this.TermReqID, this.GetTermDetails.RosettaID, this.UserDN , DateAdd("m", 1, this.GetTermDetails.TermDate) ) />
                <cfset termQueries.funcInsertArchiveFolderTermSchedule( this.TermReqID, this.GetTermDetails.RosettaID, this.ARCHIVEDIR, DateAdd("yyyy", 1, this.GetTermDetails.TermDate) ) />
                <cfloop query="this.ScheduledTerm" >
                    <cfset termQueries.funcUpdateScheduledTerms( this.ScheduledTerm.ID ) />
                </cfloop>
                <cfloop query="this.SysScheduledTerm" >
                    <cfset termQueries.funcUpdateScheduledTerms( this.SysScheduledTerm.ID ) />
                </cfloop>               
                <cfset mainQueries.funcInsertAction("", 24, "", "", "", this.TermReqID ) />
                <cfset retObj.Status = 'Completed' />
                <cfset retObj.Message = "<strong>Termination is complete.</strong>" />
            <cfelseif this.Scheduled OR this.SysScheduled >
                <cfset mainQueries.funcInsertAction("", 23, "", "", "", this.TermReqID ) />
                <cfset retObj.Status = 'Completed' />
                <cfset retObj.Message = "<strong>Termination is complete.</strong>" />
            <cfelse>
                <cfset mainQueries.funcInsertAction("", 23, "", "", "", this.TermReqID ) />
                <cfset retObj.Status = 'Completed' />
                <cfset retObj.Message = "<strong>Termination is complete.</strong>" />
            </cfif>
             
            <cfcatch type="any">
                <cfset retObj.Status = 'Fail' />
                <cfset retObj.Result &= ' Message: #replace(CFCATCH.message,"'","\'","all")#<BR/>Detail: #replace(CFCATCH.Detail,"'","\'","all")#' />
                <cfset retObj.Message = 'Error Completing Termination: #replace(retObj.Result,"\'","'","all")#<BR /><BR />
                         Step ID: #this.GetTermDetails.ActionStepID#<BR />
                         Action by User (Rosetta ID): #Session.intUserRosettaID#'  />                
                <cfset mainQueries.funcInsertErrorLog(#this.GetTermDetails.ActionStepID#, '#Session.intUserRosettaID#', 
                        'Error Completing Termination:  #replace(retObj.Result,"\'","'","all")#')/>
                <cfset ErrorEmail("EPAR Error:  Error Completing Termination", 
                        retObj.Message) />                
                <Cfreturn  retObj />    
            </cfcatch>                    
        </cftry>
        
        <Cfreturn  retObj />
    </cffunction>
    
    
    <cffunction name="GetUserDNArchivedContainer" access="private" output="false" returntype="any">
        <cfargument name="UserID" type="string" required="false" default="" />
        <cfset Var retVal = {UserDN = "", ArchivedUsersContainer = ""} />        
        <cfset objMod.init("10.100.1.101") />
        <cfset retVal.UserDN = objQuery.GetProperty("distinguishedName", "10.100.1.101", "#ARGUMENTS.UserID#", "", "") />
        <cfset retVal.ArchivedUsersContainer = objMod.ARCHIVED_USERS_CONTAINER />
        <cfreturn retVal />                       
    </cffunction>
    
    
    <cffunction name="ErrorEmail" access="private" output="false" returntype="void">
        <cfargument name="EmailSubject" type="string" required="false" default="EPAR Error" />
        <cfargument name="EmailBody" type="string" required="false" default="" />
        
        <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" 
            subject="#ARGUMENTS.EmailSubject#" type="html">
            
            #ARGUMENTS.EmailBody#
	        
	    </cfmail>                
    </cffunction>
    
    
    
     <cffunction name="ServiceDeskErrorEmail" access="private" output="false" returntype="void">
        <cfargument name="EmailSubject" type="string" required="false" default="EPAR Error" />
        <cfargument name="EmailBody" type="string" required="false" default="" /> 
        <cfset toEmail = 'SCFITSServiceDeskDL@SouthcentralFoundation.com;bfrick@scf.cc' />       
        <cfmail to=#Session.SERVICEDESK_EMAIL# From="EPAR_Error@scf.cc" subject="#ARGUMENTS.EmailSubject#" type="html">            
            #ARGUMENTS.EmailBody#            
        </cfmail>                
    </cffunction>
    
    
    <cffunction name="AlertEmail" access="public" output="false" returntype="void">
        <cfargument name="EmailSubject" type="string" required="false" default="EPAR Error" />
        <cfargument name="EmailBody" type="string" required="false" default="" />
        <cfargument name="From" type="string" required="false" default="EPAR_Termination@scf.cc" />
        <cfargument name="To" type="string" required="false" default="#Session.SERVICEDESK_EMAIL#" />
        
        <cfmail to=#ARGUMENTS.To# From="#ARGUMENTS.From#" subject="#ARGUMENTS.EmailSubject#" type="html">            
            #ARGUMENTS.EmailBody#            
        </cfmail>                
    </cffunction>
    
    
    <cffunction name="funcClearErrorLog" returntype="any">
	    <cfset LOCALS.errorLog = replace(replace(replace(replace(
	            objMod.GetErrorLog(),chr(13),'<BR/>','all')
	            , chr(10), '','all'),'<BR/>',''),'<BR/><BR/>','')  />
	    <Cfset objMod.ClearErrorLog() />
	    
	    <cfreturn LOCALS.errorLog />
	</cffunction>	
	
    	
	<cffunction name="funcADDisable" returntype="numeric" access="private">
	    <cfargument name="UserID" type="string" required="yes">
	    <cfset objMod.init("10.100.1.101")>
	    
	    <cfset FUNC.Result = 0>

	        <!--- Disable  --->
	        <cfset FUNC.Result += objMod.ModifyUser(
	            "#ARGUMENTS.UserID#", "userAccountControl", JavaCast("int", 514)) 
	        /> 
	
	        <!--- Scramble PW --->
	        <cfset FUNC.Result += objMod.ChangePW(
	            "#ARGUMENTS.UserID#", "") 
	        />
	
	        <!--- Hide from Address List --->
	        <cfset FUNC.Result += objMod.ModifyUser(
	            "#ARGUMENTS.UserID#", "MSExchHideFromAddressLists", JavaCast("boolean", True)) 
	        />  
	
	    <cfreturn FUNC.Result>
	</cffunction>
	
	
	<cffunction name="funcArchiveHomeFolder" returntype="struct" access="private">
	    <cfargument name="Home" type="string" required="yes">
	    <cfargument name="ArchiveDir" type="string" required="yes">
	    <cfset FUNC = {
	        Src = "",
	        Dest = "",
	        ArchResult ="",
	        Result = 0
	    } />
	    
	    <!--- Archive the Home Folder --->      
	    <cfif DirectoryExists(Home)> 
	        <cfdirectory action="list" directory="#ARGUMENTS.Home#" 
	            recurse="yes" name="FUNC.Contents" >
	        
	        <cfloop query="FUNC.Contents">
	            <cfset FUNC.Src = FUNC.Contents.Directory & "\" & FUNC.Contents.Name>
	            <cfset FUNC.ArchResult ="Archiving: #FUNC.Src#">
	            <cfset FUNC.Dest = ARGUMENTS.ArchiveDir & "\" & right(FUNC.Src, Len(FUNC.Src) - Len(ARGUMENTS.Home)) />
	            
	            <cfif Type EQ "Dir">
	                <cfdirectory action="create" directory="#FUNC.Dest#">
	            <cfelseif Type EQ "File">
	                <cffile action="copy" source="#FUNC.Src#" 
	                    destination="#FUNC.Dest#" >
	            </cfif>
	        
	        </cfloop>    
	                
	            <!--- Delete the Original --->  
	            <cfdirectory action="delete" directory="#ARGUMENTS.Home#" recurse="yes">
	            <cfset FUNC.ArchResult ='Archive Complete. Original directory (#ARGUMENTS.Home#) removed.'>
	        
	    <cfelse>
	        <cfset FUNC.ArchResult = "Directory does not exist #ARGUMENTS.Home#">
	    </cfif>     
	    
	    <cfreturn FUNC>    
	</cffunction>
		
	
	<cffunction name="funcHRTerm" returntype="struct" access="private">
	    <cfargument name="PersonIDNo" type="numeric" required="yes">
	    <cfargument name="EmpStatusIDNo" type="numeric" required="yes">
	    <cfargument name="OrgSCF" type="numeric" required="yes">
	    <cfargument name="RehireElig" type="numeric" required="yes"> <!---BIT--->
	    <cfargument name="TermTypeID" type="numeric" required="yes">
	    <cfargument name="EmpStatusReason" type="numeric" required="yes">
	    <cfargument name="ActionID" type="numeric" required="yes">
	    <cfargument name="Term" type="string" required="yes">
	    <cfargument name="COBRA" type="string" required="yes">
                    
	    <cfset LOCALS.SQL= "scf_SCFVM18_Termination #ARGUMENTS.PersonIDNo#, 
	        #ARGUMENTS.EmpStatusIDNo#, 
	        #ARGUMENTS.OrgSCF#,
	        #ARGUMENTS.RehireElig#,
	        #ARGUMENTS.TermTypeID#,
	        #ARGUMENTS.EmpStatusReason#,
	        #ARGUMENTS.ActionID#,
	        '#ARGUMENTS.Term#',
	        '#ARGUMENTS.COBRA#'" />
	        
	    <cfset thread.SQL = LOCALS.SQL>  <!---Conditional statement in case this function errors out --->
	    
	    <cfstoredproc datasource="#Session.DataSource#" procedure="scf_SCFVM18_Termination"> 
	        <cfprocparam cfsqltype="cf_sql_integer" value=#ARGUMENTS.PersonIDNo#>                           
	        <cfprocparam cfsqltype="cf_sql_integer" value=#ARGUMENTS.EmpStatusIDNo#>
	        <cfprocparam cfsqltype="cf_sql_integer" value=#ARGUMENTS.OrgSCF#>
	        <cfprocparam cfsqltype="cf_sql_bit" value=#ARGUMENTS.RehireElig#>
	        <cfprocparam cfsqltype="cf_sql_integer" value=#ARGUMENTS.TermTypeID#>
	        <cfprocparam cfsqltype="cf_sql_integer" value=#ARGUMENTS.EmpStatusReason#>
	        <cfprocparam cfsqltype="cf_sql_integer" value=#ARGUMENTS.ActionID#>
	        <cfprocparam cfsqltype="cf_sql_date" value="#ARGUMENTS.Term#">
	        <cfprocparam cfsqltype="cf_sql_date" value="#ARGUMENTS.COBRA#">
	        <cfprocresult name="Result1" resultset="1">
	        <cfprocresult name="Result2" resultset="2">
	        <cfprocresult name="Result3" resultset="3">
	        <cfprocresult name="Result4" resultset="4">
	        <cfprocresult name="ErrorCheck" resultset="5">
	    </cfstoredproc>
	    
	    <cfset LOCALS.Result = ErrorCheck.Error>
	        
	    <cfreturn LOCALS />
	    
	</cffunction>
		
	
	<cffunction name="funcDisableTimeKeeper" returntype="void" access="private">
	    <cfargument name="WFCPersonID" type="numeric" required="yes">
	            
	    <!--- Disable TimeKeeper Record --->        
	    <cfquery datasource="#Session.DataSource#" name="qryDisableTK">
	        scf_SCFVM18_RenameKronosUser #ARGUMENTS.WFCPersonID#
	    </cfquery>  
	    
	</cffunction>
		
	
	<cffunction name="funcExportMailbox" returntype="struct" access="private">
	    <cfargument name="UserID" type="string" required="yes">
	    <cfargument name="ArchiveEmailDir" type="string" required="yes">
	    <cfargument name="SERVICE_ACCOUNT" type="string" required="yes">
        
	    <!--- Export the Mailbox --->   
	    <cfset FUNC.Result = objTerm.ExportMailbox(
	        "#ARGUMENTS.UserID#", 
	        "#ARGUMENTS.ArchiveEmailDir#\#ARGUMENTS.UserID#.pst",
	        "#ARGUMENTS.SERVICE_ACCOUNT#")> 
	        
	    <cfset FUNC.Error = objTerm.GetErrorText()>
	    
	    <cfreturn FUNC>                         
	</cffunction>
		
	
	<cffunction name="funcArchiveAndDisable" returntype="struct" access="private">
	    <cfargument name="UserID" type="string" required="yes">
	    <cfset objMod.init("10.100.1.101")>    
	    <cfset FUNC.DN = "">	    
	    <!--- Archive the Account --->  
	    <cfset FUNC.DN = objQuery.GetProperty("distinguishedName", "10.100.1.101", "#ARGUMENTS.UserID#", "", "")>
	    <cfset FUNC.Result = objMod.MoveToContainer("#FUNC.DN#", "#objMod.ARCHIVED_USERS_CONTAINER#")>
	    <cfset FUNC.ArchivedUsersContainer = objMod.ARCHIVED_USERS_CONTAINER />	    
	    <!--- Disable.  It seems during testing that the account can be enabled from time to time.  Lets make sure  --->
	    <cfset FUNC.Result += objMod.ModifyUser("#ARGUMENTS.UserID#", "userAccountControl", JavaCast("int", 514))> 	    
	    <cfset FUNC.Error = objMod.GetErrorLog()>	    
	    <cfset FUNC.objMod = objMod />	    
	    <cfreturn FUNC> 
	                            
	</cffunction>
		
	
	<cffunction name="funcDisableRosetta" returntype="void" access="private">    
	    <cfargument name="RosettaID" type="string" required="yes">
	    
	    <!--- Disable Rosetta Record --->
	    <cfquery name="qryDisableRosettaRecord" datasource="#Session.DataSourceRosetta#">
	        scf_DisableRosettaUser #ARGUMENTS.RosettaID#
	    </cfquery>
	</cffunction>
    
</cfcomponent>